<?php
/**
 * 
 * Контроллер нод
 * @author andrew
 *
 */
class Pages_NodeController extends Zend_Controller_Action {
	/**
	 * 
	 * Собирает все ноды для страницы
	 * @throws Zend_Controller_Action_Exception
	 */
	public function indexAction() {
		$page_id = $this->_request->getParam('id',0);
		
		$page = Doctrine_Core::getTable('Pages_Model_Page')->find($page_id);
		
		if(!$page_id || !$page)
			throw new Zend_Controller_Action_Exception('Page not found', 404);
			
		$nodes = Doctrine_Query::create()
				 ->from('Pages_Model_Node')
				 ->where('page_id = ?', $page_id)
				 ->orderBy('ordering ASC')
				 ->execute();
		$this->view->page_id = $page_id;
		$this->view->nodes = $nodes;
	}
	/**
	 * 
	 * Создаем ноду
	 * @throws Zend_Controller_Action_Exception
	 */
	public function createAction() {
		$page_id = $this->_request->getParam('id',0);
		
		$page = Doctrine_Core::getTable('Pages_Model_Page')->find($page_id);
		
		if(!$page_id || !$page)
			throw new Zend_Controller_Action_Exception('Page not found', 404);
		
		$pages = Doctrine_Core::getTable('Pages_Model_Page')->findAll();
		
		$populate = array();
		
		foreach ($pages as $page) {
			$populate[$page->id] = $page->Translation[$this->view->LANG]->name;
		}
		$form = new Pages_Form_Node();
		$form->setAction($this->view->url(array('id' => $page_id),'pages_node_create'));
		$page_element = $form->getElement('page_id');
		$page_element->addMultiOptions($populate);
		$page_element->setValue($page_id);
				
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			$node = new Pages_Model_Node();
			
			foreach ($values['title'] as $lang=>$value) {
				$node->Translation[$lang]->title = $value;	
			}
			foreach ($values['content'] as $lang=>$value) {
				$node->Translation[$lang]->content = $value;	
			}
			$node->page_id = $values['page_id'];
			
			$maxOrdering = Doctrine_Query::create()
						   ->select('MAX(ordering) as maxordering')
						   ->from('Pages_Model_Node')
						   ->where('page_id = ?', $page_id)
						   ->execute();
			if($maxOrdering->count() > 0) {
				$node->ordering = $maxOrdering[0]->maxordering + 1;
			}
			
			$node->save();
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Node has been save successfully!'));
			return $this->_redirect($this->view->url(array('id' => $page_id),'pages_node_index'));
		}
		
		$this->view->form = $form;
	}
	/**
	 * 
	 * Обновление ноды
	 */
	public function updateAction() {
		$page_id = $this->_request->getParam('id',0);
		$node_id = $this->_request->getParam('node_id',0);
		
		$node = Doctrine_Core::getTable('Pages_Model_Node')->find($node_id);
		
		$page = Doctrine_Core::getTable('Pages_Model_Page')->find($page_id);
		
		if(!$page_id || !$page || !$node_id || !$node)
			throw new Zend_Controller_Action_Exception('Page or node not found', 404);
		
		$pages = Doctrine_Core::getTable('Pages_Model_Page')->findAll();
		
		$populate = array();
		
		foreach ($pages as $page) {
			$populate[$page->id] = $page->Translation[$this->view->LANG]->name;
		}
		$form = new Pages_Form_Node();
		$form->setAction($this->view->url(array('id' => $page_id, 'node_id' => $node_id),'pages_node_update'));
		$page_element = $form->getElement('page_id');
		$page_element->addMultiOptions($populate);
		
		$populate = array(
			'page_id' => $node->page_id
		);
		
		$locales = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getOption('locales');
		
		foreach ($locales as $lang=>$value) {
			$populate['content'][$lang] = $node->Translation[$lang]->content;
			$populate['title'][$lang] = $node->Translation[$lang]->title;
		}
		
		$this->view->form = $form;
		
		if($this->_request->isPost() && $form->isValid($this->_request->getPost())) {
			$values = $form->getValues();
			
			foreach ($values['title'] as $lang=>$value) {
				$node->Translation[$lang]->title = $value;	
			}
			foreach ($values['content'] as $lang=>$value) {
				$node->Translation[$lang]->content = $value;	
			}
			$node->page_id = $values['page_id'];
			
			$node->save();
			$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Node has been save successfully!'));
			return $this->_redirect($this->view->url(array('id' => $page_id),'pages_node_index'));
		} else {
			$form->populate($populate);
		}
	}
	/**
	 * 
	 * Удаление ноды
	 * @throws Zend_Controller_Action_Exception
	 */
	public function deleteAction() {
		$page_id = $this->_request->getParam('id',0);
		$node_id = $this->_request->getParam('node_id',0);
		
		$node = Doctrine_Core::getTable('Pages_Model_Node')->find($node_id);
		
		$page = Doctrine_Core::getTable('Pages_Model_Page')->find($page_id);
		
		if(!$page_id || !$page || !$node_id || !$node)
			throw new Zend_Controller_Action_Exception('Page or node not found', 404);
			
		$node->delete();
		$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Node has been deleted successfully!'));
		return $this->_redirect($this->view->url(array('id' => $page_id),'pages_node_index'));
	}
	
	public function orderingAction() {
		$page_id = $this->_request->getParam('id',0);
		$node_id = $this->_request->getParam('node_id',0);
		
		$node = Doctrine_Core::getTable('Pages_Model_Node')->find($node_id);
		
		$page = Doctrine_Core::getTable('Pages_Model_Page')->find($page_id);
		
		if(!$page_id || !$page || !$node_id || !$node)
			throw new Zend_Controller_Action_Exception('Page or node not found', 404);
			
		$dir = $this->_request->getParam('dir','1');
		
		if($dir == '1') {
			$upingPages = Doctrine_Query::create()
						  ->from('Pages_Model_Node')
						  ->where('page_id = ?',$page_id)
						  ->andWhere('ordering <= ?', $node->ordering)
						  ->andWhere('id <> ?', $node->id)
						  ->orderBy('ordering ASC')
						  ->execute();
			if($upingPages->count() > 0) {
				$upingPage = $upingPages[0];
				$ordering = $node->ordering;
				$node->ordering = $upingPage->ordering;
				$node->save();
				$upingPage->ordering = $ordering;
				$upingPage->save();
				
			}
		}
		if($dir == '-1') {
			$downingPages = Doctrine_Query::create()
						  ->from('Pages_Model_Node')
						  ->where('page_id = ?',$page_id)
						  ->andWhere('ordering >= ?', $node->ordering)
						  ->andWhere('id <> ?', $node->id)
						  ->orderBy('ordering ASC')
						  ->execute();
			if($upingPages->count() > 0) {
				$upingPage = $downingPages[0];
				$ordering = $node->ordering;
				$node->ordering = $upingPage->ordering;
				$node->save();
				$upingPage->ordering = $ordering;
				$upingPage->save();
				
			}
		}
		$this->_helper
			     ->getHelper('FlashMessenger')
				 ->addMessage($this->view->translate('Node has been moving successfully!'));
		return $this->_redirect($this->view->url(array('id' => $page_id),'pages_node_index'));
	}
}